//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.planw.beetl.utils;

import com.intellij.openapi.module.JavaModuleType;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.model.MavenArtifact;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.MavenProjectsManager;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MavenUtil {
    public MavenUtil() {
    }

    public void getResourcePath(Project project, PsiFile containingFile) {
        Module module = ModuleUtil.findModuleForFile(containingFile.getOriginalFile());
        MavenProject mavenProject = MavenProjectsManager.getInstance(project).findProject(module);
    }

    public static int getBeetlSqlVersion(Project project) {
        Set<String> versionSet = new HashSet();
        MavenProjectsManager mavenProjectsManager = MavenProjectsManager.getInstance(project);
        for (Module module : ModuleUtil.getModulesOfType(project, new JavaModuleType())) {
            if (mavenProjectsManager.isMavenizedProject()) {
                MavenProject mavenProject = mavenProjectsManager.findProject(module);
                System.out.println("mavenProject：" + mavenProject);
                if (mavenProject != null) {
                    List<MavenArtifact> mavenArtifacts = mavenProject.findDependencies("com.ibeetl", "beetlsql");
                    if (!mavenArtifacts.isEmpty()) {
                        for (MavenArtifact mavenArtifact : mavenArtifacts) {
                            String version = mavenArtifact.getVersion();
                            if (version.startsWith("3")) {
                                versionSet.add("3");
                            }

                            if (version.startsWith("2")) {
                                versionSet.add("2");
                            }
                        }
                    }
                }
            } else {
                versionSet.add(getBeetlSqlVersionByLib(project));
            }
        }
        System.out.println("versionSet:" + versionSet);
        return !versionSet.isEmpty() && !versionSet.contains("2") ? 3 : 2;
    }

    public static String getBeetlSqlVersionByLib(Project project) {
        // 获取项目的 lib 目录（需替换为实际路径）
        AtomicReference<String> version = new AtomicReference();
        VirtualFile libDir = LocalFileSystem.getInstance().findFileByPath(project.getBasePath() + "/libs");
        if (libDir != null && libDir.isDirectory()) {
            Arrays.stream(libDir.getChildren())
                    .filter(file -> file.getExtension() != null && file.getExtension().equalsIgnoreCase("jar"))
                    .filter(file -> file.getName().startsWith("beetlsql")) // 过滤 beetlsql 开头的 JAR
                    .forEach(file -> {
                        String fileName = file.getName();
                        // 匹配 beetlsql-{version}.jar 格式（兼容 2.x/3.x）
                        Matcher matcher = Pattern.compile("beetlsql-(\\d+\\..+?)(\\.RELEASE)?.jar").matcher(fileName);
                        if (matcher.find()) {
                            version.set(matcher.group(1).startsWith("3") ? "3" : "2");
                        }
                    });
        }
        return version.get();
    }

    public static boolean isDependOnBeetlSql(@Nullable Module module) {
        if (module == null) {
            return false;
        } else {
            Project project = module.getProject();
            MavenProjectsManager mavenProjectsManager = MavenProjectsManager.getInstance(project);
            MavenProject mavenProject = mavenProjectsManager.findProject(module);
            List<MavenArtifact> mavenArtifacts = mavenProject != null ? mavenProject.findDependencies("com.ibeetl", "beetlsql") : Collections.emptyList();
            return !mavenArtifacts.isEmpty();
        }
    }
}
